package com.xiezc.util

//

import java.security.MessageDigest
import java.util.*

class MD5Util {
    companion object {
        private val ALGORITHM = "MD5"
        private val DEFAULT_CHARSET = "UTF-8"
        private val hexDigits = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')

        @Throws(Exception::class)
        fun md5(source: ByteArray): String {
            val messageDigest = MessageDigest.getInstance(ALGORITHM)
            messageDigest.update(source)
            val digest = messageDigest.digest()
            val j = digest.size
            val str = CharArray(j * 2)
            var k = 0

            for (i in 0..j - 1) {
                val byte0 = digest[i]
                str[k++] = hexDigits[byte0.toInt().ushr(4) and 15]
                str[k++] = hexDigits[byte0.toInt() and 15]
            }

            return String(str)
        }

        @Throws(Exception::class)
        fun md5(text: String): String {
            return md5(text, DEFAULT_CHARSET)
        }

        @Throws(Exception::class)
        fun md5(str: String, charset: String): String {
            return md5(str.toByteArray(charset(charset)))
        }
    }
}

fun main(args: Array<String>) {
    println(UUID.randomUUID().toString())
}
